// src/views/ParkingSpotManagement.vue
<template>
  <div class="parking-spot-management">
    <h1>车位管理</h1>
    <el-button @click="handleOpenAddDialog">添加车位</el-button>
    <el-table :data="parkingSpots" style="width: 100%">
      <el-table-column prop="location" label="位置"></el-table-column>
      <el-table-column prop="isOccupied" label="是否被占用" :formatter="formatOccupied"></el-table-column>
      <el-table-column label="操作">
        <template v-slot:default="scope">
          <el-button @click="handleOpenEditDialog(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑车位对话框 -->
    <el-dialog title="车位信息" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="rules" ref="parkingSpotForm">
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location"></el-input>
        </el-form-item>
        <el-form-item label="是否被占用" prop="isOccupied">
          <el-switch v-model="form.isOccupied"></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveParkingSpot">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      parkingSpots: [],
      dialogVisible: false,
      form: {
        id: null,
        location: '',
        isOccupied: false
      },
      rules: {
        location: [{ required: true, message: '请输入位置', trigger: 'blur' }]
      }
    };
  },
  methods: {
    fetchParkingSpots() {
      axios.get('/api/parking-spots/')
        .then(response => {
          this.parkingSpots = response.data;
        })
        .catch(error => {
          console.error('Error fetching parking spots:', error);
        });
    },
    handleOpenAddDialog() {
      this.form = { id: null, location: '', isOccupied: false };
      this.dialogVisible = true;
    },
    handleOpenEditDialog(parkingSpot) {
      this.form = { ...parkingSpot };
      this.dialogVisible = true;
    },
    handleDelete(id) {
      axios.delete(`/api/parking-spots/${id}`)
        .then(() => {
          this.parkingSpots = this.parkingSpots.filter(spot => spot.id !== id);  // Local update
          this.$message.success('车位删除成功');
        })
        .catch(error => {
          console.error('Error deleting parking spot:', error);
          this.$message.error('车位删除失败');
        });
    },
    saveParkingSpot() {
      const method = this.form.id ? 'put' : 'post';
      const url = this.form.id ? `/api/parking-spots/${this.form.id}` : '/api/parking-spots/';
      axios[method](url, this.form)
        .then(() => {
          this.dialogVisible = false;
          this.fetchParkingSpots();  // Refresh data
          this.$message.success('车位保存成功');
        })
        .catch(error => {
          console.error('Error saving parking spot:', error);
          this.$message.error('车位保存失败');
        });
    },
    formatOccupied(value) {
      return value ? '被占用' : '空闲';
    }
  },
  created() {
    this.fetchParkingSpots();
  }
}
</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>
